package com.itheima.controller;


import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {


    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 上传到阿里云oss
     * @param image
     * @return
     * @throws Exception
     */

    @PostMapping
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传成功 {}",image.getOriginalFilename());
        String originalFilename = image.getOriginalFilename();
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }

    /**
     * 本地存储
     * @param name
     * @param age
     * @param image
     * @return
     * @throws Exception
     */
//    @PostMapping
//    public Result upload1(String name, Integer age, MultipartFile image) throws Exception {
//        log.info("文件上传成功 {},{},{}",name,age,image);
//
//        String originalFilename = image.getOriginalFilename();
//        int index = originalFilename.lastIndexOf(".");
//        String extname = originalFilename.substring(index);
//        String filename = UUID.randomUUID().toString() + extname;
//        image.transferTo(new File("D:\\360Downloads\\"+filename));
//        return Result.success();
//    }
}
